//index.js 获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    winWidth: '',
    winHeight: '',
    code: '',
    encryptedData: '',
    iv: '',
    sessionId: '',
    setps: '',
    userm: '',
    date: '',
    usertype: false,
    adds: {},
    myadd: '',
    sport: {},
    minqw: '0',
    maxqw: '0',
    array: 0,
    arrays: '',
    animationData: {},
    bgimage: '/image/bg.jpg',
    canvass: false,
    pageinfo: {},
    btnType: true,
    openid: ''
    // hacker: "hacker"
  },

  onShow: function() {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        console.log(latitude + ',' + longitude)
        wx.request({
          url: app.APIURL+'getTq',
          type: 'get',
          data: {
            location: latitude + ',' + longitude
          },
          success: function(res) {
            console.log(res)
            that.setData({
              minqw: res.data.HeWeather6[0].daily_forecast[0].tmp_min,
              maxqw: res.data.HeWeather6[0].daily_forecast[0].tmp_max,
              date: util.getData(),
              myadd: res.data.HeWeather6[0].basic.cnty + ' | ' + res.data.HeWeather6[0].basic.admin_area + ' | ' + res.data.HeWeather6[0].basic.location,
              sport: res.data.HeWeather6[0].lifestyle[3]
            })
          }
        })
      }
    })
  },
  //事件处理函数
  onReady: function(e) {},
  onLoad: function(e) {

    // util.showLogin('正在加载')
    // wx.showLoading({
    //   title: '加载中'
    // })
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'getUserInfo',
    }).then(
      res => {
        console.log(res)
        app.openId = res.result.openId
        this.setData({
          openid: res.result.openId
        })

        wx.getSetting({
          success: (res) => {
              wx.login({
                success: res => {
                  console.log(res)
                  this.guser(res.code)
                }
              })
          }
        })
      }
    )



    wx.getSystemInfo({
      success: res => {
        wx.setStorageSync('w', res.windowWidth)
        wx.setStorageSync('y', res.windowHeight)
        this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        })
      }
    })
  },
  guser: function(code) {
    let that = this
    wx.getUserInfo({
      success: data => {
        console.log(data)
        that.setData({
          userInfo: data.userInfo
        })
        wx.request({
          url: app.APIURL+'regUser',
          type: 'get',
          data: {
            avatarUrl: data.userInfo.avatarUrl,
            nickName: data.userInfo.nickName,
            openId: that.data.openid,
            gender: data.userInfo.gender,
          },
          success: function(res) {
            console.log(res)

          }
        })

        wx.request({
          url: app.APIURL+'GetUserInfo',
          type: 'get',
          data: {
            code: code
          },
          success: function(s) {
            console.log(s)
            wx.getWeRunData({
              success(res) {
                // const encryptedData = res.encryptedData
                console.log(res)
                wx.request({
                  url: app.APIURL+'gets',
                  type: 'get',
                  data: {
                    session: s.data.session_key,
                    iv: res.iv,
                    encryptData: res.encryptedData,
                  },
                  success: rr => {
                    let num = rr.data.stepInfoList.length -1 ;
                    console.log(rr.data.stepInfoList.length)
                    that.setData({
                      setps: rr.data.stepInfoList[num].step,
                      userm: util.stepzh(rr.data.stepInfoList[num].step),
                      array: Number(util.usermm(rr.data.stepInfoList[num].step))
                    })
                    wx.request({
                      url: app.APIURL+'RecordStep',
                      type:'get',
                      data:{
                        'openId':that.data.openid,
                        'setp': that.data.setps,
                        // 'days': util.formatTime()
                        // 'days': '2019-04-10'
                      },success:function(res){
                        console.log(res)
                        console.log(util.formatTime())
           
                      }
                    })
                  }
                })
              }
            })
          }
        })
      }
    })
  },
  gotoRanking: function() {
    wx.navigateTo({
      url: '../ranking/index'
    })
  },
  saveImage: function() { //保存图片
    console.log("asdf");
    var myDate = new Date();
    // const dtime = myDate.getHours();
    var thisTime = myDate.toLocaleString()

    wx.setStorageSync('username', this.data.userInfo.nickName); //名字
    wx.setStorageSync('add', this.data.myadd); //地址
    wx.setStorageSync('xx', this.data.array); //星星数量
    wx.setStorageSync('setp', this.data.setps); //步数
    wx.downloadFile({
      url: this.data.userInfo.avatarUrl,
      success: imgmess => {
        wx.setStorageSync('userimg', imgmess.tempFilePath)
      }
    });
    // wx.setStorageSync('userimg', this.data.userInfo.avatarUrl)
    wx.setStorageSync('time', thisTime); //时间
    wx.setStorageSync('titlebg', '/image/fximg/1.png')

    wx.navigateTo({
      url: '../saves/index'
    })
  },

  onShareAppMessage: function(res) {
    return {
      title: wx.getStorageSync('username') + '今天走了' + this.data.userm + '米,不服来战！',
      path: '/pages/index/index',
      // imageUrl: wx.getStorageSync('titleimg'),
      imageUrl: '/image/title.jpg',
      success: function(res) {
        console.log(wx.getStorageSync('m'))
        // 转发成功
        wx.showModal({
          title: 'LMOMO的程序提示',
          content: '感谢您的转发，以后可以和你的好友进行PK、打卡朋友圈了！',
          success: function(res) {
            if (res.confirm) {} else if (res.cancel) {
              // console.log('用户点击取消')
            }
          }
        })
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})